package com.example.getwifiip.utils

import java.io.BufferedReader
import java.io.DataOutputStream
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL
import java.net.URLEncoder
import java.util.concurrent.Callable
import java.util.concurrent.FutureTask

class HttpConnectionUtil {

    fun getRequest(url: String): String? {
        val sb = StringBuilder()
        val task = FutureTask(Callable {
            var connection: HttpURLConnection? = null
            var reader: BufferedReader? = null
            try {
                val requestUrl = URL(url)
                connection = requestUrl.openConnection() as HttpURLConnection
                connection.requestMethod = "GET"
                connection.connectTimeout = 8000
                connection.readTimeout = 8000
                if (connection.responseCode == 200) {
                    val input = connection.inputStream
                    reader = BufferedReader(InputStreamReader(input))
                    var line: String
                    do {
                        line = reader.readLine()
                        if (line == null) {
                            break
                        }
                        sb.append(line)
                    } while (true)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                reader?.close()
                connection?.disconnect()
            }
            sb.toString()
        })
        Thread(task).start()
        var s: String? = null
        try {
            s = task.get()
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return s
    }

    fun postRequest(url: String, map: Map<String, String>): String? {
        val sb = StringBuilder()
        val task = FutureTask(Callable {
            var connection: HttpURLConnection? = null
            var reader: BufferedReader? = null
            try {
                val requestUrl = URL(url)
                connection = requestUrl.openConnection() as HttpURLConnection
                connection.requestMethod = "POST"
                connection.connectTimeout = 8000//链接超时
                connection.readTimeout = 8000//读取超时
                //发送post请求必须设置
                connection.doOutput = true
                connection.doInput = true
                connection.useCaches = false
                connection.instanceFollowRedirects = true
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
                val out = DataOutputStream(
                    connection
                        .outputStream
                )
                val request = StringBuilder()
                for (key in map.keys) {
                    request.append(key + "=" + URLEncoder.encode(map[key], "UTF-8") + "&")
                }
                out.writeBytes(request.toString())//写入请求参数
                out.flush()
                out.close()
                if (connection.responseCode == 200) {
                    val input = connection.inputStream
                    reader = BufferedReader(InputStreamReader(input))
                    var line: String
                    do {
                        line = reader.readLine()
                        if (line == null) {
                            break
                        }
                        sb.append(line)
                    } while (true)
                    println(sb)

                }
            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                reader?.close()
                connection?.disconnect()
            }
            sb.toString()
        })
        Thread(task).start()
        var s: String? = null
        try {
            s = task.get()
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return s
    }

    companion object {
        var http = HttpConnectionUtil()
    }

}